﻿//  #region execute extension method
$.execute = function (methodName, parameters, options) {
    ///	<summary>
    ///		Call server side method inside current AjaxAvailableControl
    ///	</summary>
    ///	<param name="methodName" type="Object">
    ///		methodName desribes the server side method name, for example,
    ///     "DemoMethod2"
    ///	</param>
    ///	<param name="parameters" type="Object">
    ///		Parameters argument desribes the parameters collection to be sent to server side method as collection of key+value pairs, for example, 
    ///     { "id", 1 }, { "name", "test" }
    ///	</param>
    ///	<param name="options" type="Object">
    ///		Options defines onsuccess and onError handlers to be called in approriate cases after server side method call, for instance,
    ///		{
    ///		            onSuccess: function(data) {
    ///		                alert(deserialized_request.AdditionalSettings);
    ///		            }
    ///		            onError: function(data) {
    ///		                alert("Exception thrown");
    ///		            }
    ///	</param>
    ///	<returns type="undefined">
    var settings = jQuery.extend({
        control_id: null,
        onSuccess: function (data) { },
        onError: function (data) { alert("Exception thrown ->" + data); }
    }, options || {});

    var localOnSuccessHandler = function (data) {
        ///	<summary>
        ///		This is local callback function which will be called in case of successfull server side method call through ajaxRequest function invocation
        ///	</summary>
        ///	<param name="data" type="String">
        ///		Result as string from server side, it will be serialized value
        ///	</param>
        ///	<returns type="undefined" />
        var deserialized_request = $.JSON.decode(data);
        settings.onSuccess(deserialized_request);

    }

    var localOnErrorHandler = function (exception) {
        ///	<summary>
        ///		This is local callback function which will be called in case of UNsuccessfull server side method call through ajaxRequest function invocation
        ///	</summary>
        ///	<param name="data" type="String">
        ///		Exception message of error string as result from server side 
        ///	</param>
        ///	<returns type="undefined" />
        settings.onError(exception);
    }

    var arrayParameters = [];
    $.each(parameters, function (name, value) {
        var wrapperParameter =
        { "Key": name,
            "Value": value
        };
        arrayParameters.push(wrapperParameter);
    });
    var request = {
        MethodName: methodName,
        Parameters: arrayParameters
    };

    if (ajaxRequest)
        ajaxRequest(settings.control_id, request, localOnSuccessHandler, localOnErrorHandler);
}
//  #endregion

